#pragma once 
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Comm.hpp"
#include "Log.hpp"

const std::string gpath = "./";
const std::string gfilename = "dict.txt";
const std::string gsep = ": ";

class Dictionary
{
private:
    bool LoadDictionary()
    {
        std::string file = gpath + gfilename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file" << file << "error";
            return false;
        }
        std::string word;
        while (std::getline(in, word))// 重载了bool类型
        {
            std::string key;
            std::string value;
            if(Divide(word, gsep, &key, &value))
                _dictionary.insert(std::make_pair(key, value));
        }
        in.close();
        return true;
    }
public:
    Dictionary(std::string path = gpath, std::string filename = gfilename)
        :_path(path)
        ,_filename(filename)
    {
        LoadDictionary();
    }

    std::string Translate(const std::string &word)
    {
        auto ret = _dictionary.find(word);
        if (ret == _dictionary.end())
            return "None";
        return ret->second;
    }
    ~Dictionary()
    {
        
    }
private:
    std::unordered_map<std::string, std::string> _dictionary;
    std::string _path;
    std::string _filename;
};